package com.riggie.extension.controller;

import com.riggie.extension.constant.Constants;
import jakarta.annotation.Resource;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageDeliveryMode;
import org.springframework.amqp.core.MessageProperties;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;

@RequestMapping("/product")
@RestController
public class ProducerController {
    @Resource(name = "rabbitTemplate")
    private RabbitTemplate rabbitTemplate;
    @Resource(name = "confirmRabbitTemplate")
    private RabbitTemplate confirmRabbitTemplate;
    @Resource(name = "confirmCachingRabbitTemplate")
    private RabbitTemplate confirmCachingRabbitTemplate;
    @Resource(name = "transRabbitTemplate")
    private RabbitTemplate transRabbitTemplate;

    @RequestMapping("/ack")
    public String ack() {
        rabbitTemplate.convertAndSend(Constants.ACK_EXCHANGE,"ack","consumer ack mode test...");
        return "发送消息成功";
    }

    @RequestMapping("/pres")
    public String pres(){
        Message message = new Message("Presistent test...".getBytes(),new MessageProperties());
        //消息非持久化
        message.getMessageProperties().setDeliveryMode(MessageDeliveryMode.NON_PERSISTENT);
        //消息持久化
        //message.getMessageProperties().setDeliveryMode(MessageDeliveryMode.PERSISTENT);
        System.out.println(message);
        rabbitTemplate.convertAndSend(Constants.PRES_EXCHANGE,"pres",message);
        return "消息发送成功";
    }

    @RequestMapping("/confirm")
    public String confirm(){
        CorrelationData correlationData = new CorrelationData("1");
        confirmRabbitTemplate.convertAndSend("confirm1","confirm","confirm test...",correlationData);
        return "消息发送成功";
    }

    @RequestMapping("/msgReturn")
    public String msgReturn(){
        CorrelationData correlationData = new CorrelationData("2");
        confirmRabbitTemplate.convertAndSend(Constants.CONFIRM_EXCHANGE,"confirm11","message return test..",correlationData);
        return "消息发送成功";
    }

    @RequestMapping("/retry")
    public String retry(){
        System.out.println("retry...");
        rabbitTemplate.convertAndSend(Constants.RETRY_EXCHANGE,"retry","retry test ...");
        return "消息发送成功";
    }

    @RequestMapping("/ttl")
    public String ttl(){
        System.out.println("ttl");

        rabbitTemplate.convertAndSend(Constants.TTL_EXCHANGE,"ttl","ttl test 10s ...", message -> {
            message.getMessageProperties().setExpiration("10000");
            return message;
        });

        rabbitTemplate.convertAndSend(Constants.TTL_EXCHANGE,"ttl","ttl test 30s ...", message -> {
            message.getMessageProperties().setExpiration("30000");
            return message;
        });

        return "消息发送成功";
    }

    @RequestMapping("/ttl2")
    public String ttl2(){
        System.out.println("ttl2");
        //发送普通消息
        rabbitTemplate.convertAndSend(Constants.TTL_EXCHANGE,"ttl","ttl test ...");
        return "ttl2消息发送成功";
    }

    @RequestMapping("/dl")
    public String dl(){
        System.out.println("dl ....");
        //发送普通消息
        //rabbitTemplate.convertAndSend(Constants.NORAML_EXCHANGE,"normal","dl test..");
        System.out.printf("%tc 消息发送成功 \n", new Date());
//        //测试队列长度
//        for (int i = 0; i < 20; i++) {
//            rabbitTemplate.convertAndSend(Constants.NORAML_EXCHANGE,"normal","dl test.." + i);
//        }
//        //测试消息拒收
        rabbitTemplate.convertAndSend(Constants.NORAML_EXCHANGE,"normal","dl test..");
        return "dl消息发送成功。。";
    }


    @RequestMapping("delay")
    public String delay(){
        System.out.println("delay ....");

        rabbitTemplate.convertAndSend(Constants.NORAML_EXCHANGE,"normal","delay test ...",message -> {
            message.getMessageProperties().setExpiration("10000"); //单位：毫秒，过期时间为10s
            return message;
        });

        rabbitTemplate.convertAndSend(Constants.NORAML_EXCHANGE,"normal","delay test ...",message -> {
            message.getMessageProperties().setExpiration("30000"); //单位：毫秒，过期时间为10s
            return message;
        });

        return "delay发送成功";
    }

    @RequestMapping("delay2")
    public String delay2(){
        System.out.println("delay ....");

        rabbitTemplate.convertAndSend(Constants.NORAML_EXCHANGE,"normal","delay test ...",message -> {
            message.getMessageProperties().setExpiration("30000"); //单位：毫秒，过期时间为10s
            return message;
        });

        rabbitTemplate.convertAndSend(Constants.NORAML_EXCHANGE,"normal","delay test ...",message -> {
            message.getMessageProperties().setExpiration("10000"); //单位：毫秒，过期时间为10s
            return message;
        });

        return "delay2发送成功";
    }

    @RequestMapping("/trans")
    public String trans(){
        System.out.println("trans test ...");
        rabbitTemplate.convertAndSend("",Constants.TRANS_QUEUE,"trans test 1 ...");
        int num  = 5 / 0;
        rabbitTemplate.convertAndSend("",Constants.TRANS_QUEUE,"trans test 2 ...");
        return "消息发送成功...";
    }

    @Transactional
    @RequestMapping("/trans2")
    public String trans2(){
        System.out.println("trans test ...");
        transRabbitTemplate.convertAndSend("",Constants.TRANS_QUEUE,"trans test 1 ...");
        int num  = 5 / 0;
        transRabbitTemplate.convertAndSend("",Constants.TRANS_QUEUE,"trans test 2 ...");
        return "消息发送成功...";
    }

    @RequestMapping("/qos")
    public String qos(){
        System.out.println("qos test ...");

        //发送普通消息
        for (int i = 0; i < 20; i++) {
            rabbitTemplate.convertAndSend(Constants.QOS_EXCHANGE,"qos","qos test ..." + i);
        }

        return "qos消息发送成功";
    }

}
